package com.nexgo.payment.fragment.left;

import android.app.Dialog;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.InputType;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.nexgo.payment.R;
import com.nexgo.payment.databases.Repository;
import com.nexgo.payment.fragment.EditDialogFragment;
import com.nexgo.payment.fragment.ProgressDialogFragment;
import com.nexgo.payment.fragment.TextDialogFragment;
import com.nexgo.payment.trans.setting.SystemSettingsActivity;
import com.nexgo.payment.ui.BaseFragment;
import com.nexgo.payment.util.AppUtils;
import com.nexgo.payment.util.ToastUtils;
import com.nexgo.payment.util.UIUtils;

import java.util.Iterator;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import java8.util.Optional;

/**
 * 左侧菜单
 *
 * @author 谭忠扬-YuriTam
 * @time 2017年11月29日
 */
public class LeftFragment extends BaseFragment implements LeftContract.View {

    @BindView(R.id.tv_location)
    TextView tvLocation;
    @BindView(R.id.tv_version)
    TextView tvVersion;
    Unbinder unbinder;

    private EditDialogFragment mEditDialogFragment;
    private ProgressDialogFragment mDialogFragment;
    private LeftContract.Presenter mPresenter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        new LeftPresenter(this, Repository.getInstance());
    }

    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container) {
        View view = inflater.inflate(R.layout.fragment_main_left, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected void initData() {
        //设置当前版本
        tvVersion.setText(AppUtils.getAppVersionName(mContext));
        //自动发起定位
        mPresenter.startLocation();
    }

    @OnClick({R.id.iv_left_logo, R.id.iv_location, R.id.ll_check_update, R.id.ll_setting_param})
    public void onClicked(View view) {
        if (UIUtils.isDoubleClick()) return;
        switch (view.getId()) {
            case R.id.iv_left_logo:
                TextDialogFragment textDialogFragment = new TextDialogFragment();
                textDialogFragment.setDialogType(TextDialogFragment.DIALOG_TYPE_INFO);
                textDialogFragment.setTitleText(getString(R.string.product_introduction));
                textDialogFragment.setContentText(String.format(getString(R.string.introduction_desc), Build.MODEL), Gravity.LEFT);
                textDialogFragment.setPositionText(getString(R.string.confirm));
                textDialogFragment.show(getActivity().getSupportFragmentManager(), null);
                break;
            case R.id.iv_location:
                mPresenter.startLocation();
                break;
            case R.id.ll_check_update:
                mPresenter.checkUpdate(getActivity());
                break;
            case R.id.ll_setting_param:
                mEditDialogFragment = new EditDialogFragment();
                mEditDialogFragment.setDialogType(EditDialogFragment.DIALOG_TYPE_WARNING);
                mEditDialogFragment.addTitle(getResources().getString(R.string.pls_input_security_password));
                mEditDialogFragment.addEditText1(InputType.TYPE_CLASS_NUMBER, 8, null, true);
                mEditDialogFragment.setPositionText(getString(R.string.confirm));
                mEditDialogFragment.setNegativeText(getString(R.string.cancel));
                mEditDialogFragment.setDialogListener(new EditDialogFragment.OnDialogListener() {
                    @Override
                    public void onCancel() {
                        mEditDialogFragment.dismiss();
                    }

                    @Override
                    public void onConfirm(List<String> values) {
                        Iterator<String> iterator = values.iterator();
                        mPresenter.login2Setting(iterator.next());
                    }
                });
                mEditDialogFragment.show(getActivity().getSupportFragmentManager(), null);
                break;
            default:
                break;
        }
    }

    @Override
    public void showLocation(AMapLocation aMapLocation) {
        Optional.ofNullable(aMapLocation)
                .filter(location -> location != null && location.getErrorCode() == 0)
                .filter(location -> !TextUtils.isEmpty(location.getAddress()))
                .ifPresentOrElse(location -> tvLocation.setText(location.getAddress()),
                        () -> tvLocation.setText(R.string.get_location_fail));
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void setPresenter(LeftContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public void showIsNewestVersion() {
        ToastUtils.showToast(getString(R.string.has_no_new_version));
    }

    @Override
    public void intent2Setting() {
        if (mEditDialogFragment != null && mEditDialogFragment.getDialog() != null){
            Dialog dialog = mEditDialogFragment.getDialog();
            if (dialog.isShowing()) mEditDialogFragment.dismiss();
        }
        intent2Activity(SystemSettingsActivity.class);
    }

    @Override
    public void showErr(String errMsg) {
        Optional.ofNullable(errMsg)
                .filter(s -> !TextUtils.isEmpty(s))
                .ifPresent(s -> ToastUtils.showToast(s));
    }

    @Override
    public void showEmpty() {
        ToastUtils.showToast(getString(R.string.input_can_no_be_empty));
    }

    @Override
    public void setCheckUpdateIndicator(boolean active) {
        if (active) {
            if (mDialogFragment == null) {
                mDialogFragment = new ProgressDialogFragment();
            }
            mDialogFragment.setTipTitle(getString(R.string.check_version_update));
            mDialogFragment.setTip(getString(R.string.check_whether_has_new_version));
            mDialogFragment.setCancelable(false);
            mDialogFragment.show(getActivity().getSupportFragmentManager(), null);
        } else {
            if (mDialogFragment != null && mDialogFragment.getDialog() != null) {
                Dialog dialog = mDialogFragment.getDialog();
                if (dialog.isShowing()) {
                    mDialogFragment.dismiss();
                }
                mDialogFragment = null;
            }
        }
    }

    @Override
    public boolean isActive() {
        return isAdded();
    }
}
